package com.xbongbong.pro.message.pushset;/*/*/

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTimeTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 机会消息设置
 *
 * @author GuoJun.HU
 * @since V1.0
 * @date 2019/2/19 10:46
 */
public class OpportunityPushSet extends AbstractPushSet {

    private static Integer oneDay = 86400;

    private String pushObject;

    private static final String SPLIT = "|";



    public String getPushObject() {
        return pushObject;
    }



    public void setPushObject(String pushObject) {
        this.pushObject = pushObject;
    }

    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime,
                                         String attrEntity) {
        setPushObject(pushObject);

        PushSetEntity pushSetEntity = new PushSetEntity();

        pushSetEntity.setAddTime(addTime);
        pushSetEntity.setAttrEntity(attrEntity);
        pushSetEntity.setCorpid(corpid);
        pushSetEntity.setDel(del);
        pushSetEntity.setEnable(enable);
        pushSetEntity.setId(id);
        pushSetEntity.setPushObject(pushObject);
        pushSetEntity.setPushTime(pushTime);
        pushSetEntity.setRefEntity(refEntity);
        pushSetEntity.setSubType(subType);
        pushSetEntity.setType(type);
        pushSetEntity.setUpdateTime(updateTime);
        return pushSetEntity;
    }



    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType,
                                    PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.OPPORTUNITY_PUSH.getCode();

        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("type", type);
        param.put("subType", subType);

        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

        PushSetEntity pushSetEntity;
        if(pushSetList.isEmpty()){
            pushSetEntity = new PushSetEntity();
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(subType);
            pushSetEntity.setEnable(1);
            JSONObject pushObject = new JSONObject();
            if (Objects.equals(PushTypeEnum.OPPORTUNITY_ADD_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.OPPORTUNITY_ADD_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.OPPORTUNITY_ADD));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", PushObjectTypeEnum.MAINUSER.getName());
                jsonObject1.put("enable", 1);
                jsonObject1.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                typeArray.add(jsonObject1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                jsonObject2.put("enable", 1);
                jsonObject2.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                typeArray.add(jsonObject2);
                pushObject.put("type", typeArray);

                //manager
                JSONObject managerObject = new JSONObject();
                managerObject.put("enable", 0);
                managerObject.put("value", new ArrayList<>());
                pushObject.put("manager", managerObject);
                //dep
                JSONObject depObject = new JSONObject();
                depObject.put("enable", 0);
                depObject.put("value", new ArrayList<>());
                pushObject.put("dep", depObject);
                //role
                JSONObject roleObject = new JSONObject();
                roleObject.put("enable", 0);
                roleObject.put("value", new ArrayList<>());
                pushObject.put("role", roleObject);
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);
            } else if (Objects.equals(PushTypeEnum.OPPORTUNITY_CHANGE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.OPPORTUNITY_CHANGE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.OPPORTUNITY_CHANGE));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", PushObjectTypeEnum.MAINUSER.getName());
                jsonObject1.put("enable", 1);
                jsonObject1.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                typeArray.add(jsonObject1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                jsonObject2.put("enable", 1);
                jsonObject2.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                typeArray.add(jsonObject2);
                pushObject.put("type", typeArray);

                //manager
                JSONObject managerObject = new JSONObject();
                managerObject.put("enable", 0);
                managerObject.put("value", new ArrayList<>());
                pushObject.put("manager", managerObject);
                //dep
                JSONObject depObject = new JSONObject();
                depObject.put("enable", 0);
                depObject.put("value", new ArrayList<>());
                pushObject.put("dep", depObject);
                //role
                JSONObject roleObject = new JSONObject();
                roleObject.put("enable", 0);
                roleObject.put("value", new ArrayList<>());
                pushObject.put("role", roleObject);
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);
            } else if (Objects.equals(PushTypeEnum.OPPORTUNITY_EXPIRE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.OPPORTUNITY_EXPIRE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.OPPORTUNITY_EXPIRE));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", PushObjectTypeEnum.MAINUSER.getName());
                jsonObject1.put("enable", 1);
                jsonObject1.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                typeArray.add(jsonObject1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                jsonObject2.put("enable", 1);
                jsonObject2.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                typeArray.add(jsonObject2);
                pushObject.put("type", typeArray);

                //manager
                JSONObject managerObject = new JSONObject();
                managerObject.put("enable", 0);
                managerObject.put("value", new ArrayList<>());
                pushObject.put("manager", managerObject);
                //role
                JSONObject roleObject = new JSONObject();
                roleObject.put("enable", 0);
                roleObject.put("value", new ArrayList<>());
                pushObject.put("role", roleObject);

                /*************推送时间设置***********/
                JSONObject pushTimeObject = new JSONObject();
                //提前推送
                pushTimeObject.put("timeType", PushTimeTypeEnum.ADVANCE.getCode());
                //默认
                pushTimeObject.put("time", 0);
                //无效时间间隔
                pushTimeObject.put("period", OpportunityPushSet.oneDay);
                //循环
                pushTimeObject.put("loop", 0);
                //时间控件类型 0具体时间点 1天数
                pushTimeObject.put("type", 1);

                JSONArray timeArray = new JSONArray();
                JSONObject timeObject = new JSONObject();
                timeObject.put("id", 1);
                timeObject.put("name", I18nMessageUtil.getMessage(CommonConstant.ADVANCE));
                timeArray.add(timeObject);
                JSONObject timeObject2 = new JSONObject();
                timeObject2.put("id", 3);
                timeObject2.put("name", I18nMessageUtil.getMessage(CommonConstant.THE_DAY));
                timeArray.add(timeObject2);
                pushTimeObject.put("timeArray", timeArray);

                pushSetEntity.setPushTime(pushTimeObject.toJSONString());
            } else if (Objects.equals(PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_MAIN_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_MAIN_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.OPPORTUNITY_TEAM_ADD_DEL_MAIN));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", PushObjectTypeEnum.MAINUSER.getName());
                jsonObject1.put("enable", 0);
                jsonObject1.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                typeArray.add(jsonObject1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                jsonObject2.put("enable", 0);
                jsonObject2.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                typeArray.add(jsonObject2);
                pushObject.put("type", typeArray);

                //manager
                JSONObject managerObject = new JSONObject();
                managerObject.put("enable", 0);
                managerObject.put("value", new ArrayList<>());
                pushObject.put("manager", managerObject);
                //dep
                JSONObject depObject = new JSONObject();
                depObject.put("enable", 0);
                depObject.put("value", new ArrayList<>());
                pushObject.put("dep", depObject);
                //role
                JSONObject roleObject = new JSONObject();
                roleObject.put("enable", 0);
                roleObject.put("value", new ArrayList<>());
                pushObject.put("role", roleObject);
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);
            } else if (Objects.equals(PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_SYNERGETIC_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_SYNERGETIC_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.OPPORTUNITY_TEAM_ADD_DEL_SYNERGETIC));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", PushObjectTypeEnum.MAINUSER.getName());
                jsonObject1.put("enable", 0);
                jsonObject1.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                typeArray.add(jsonObject1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                jsonObject2.put("enable", 0);
                jsonObject2.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                typeArray.add(jsonObject2);
                pushObject.put("type", typeArray);

                //manager
                JSONObject managerObject = new JSONObject();
                managerObject.put("enable", 0);
                managerObject.put("value", new ArrayList<>());
                pushObject.put("manager", managerObject);
                //dep
                JSONObject depObject = new JSONObject();
                depObject.put("enable", 0);
                depObject.put("value", new ArrayList<>());
                pushObject.put("dep", depObject);
                //role
                JSONObject roleObject = new JSONObject();
                roleObject.put("enable", 0);
                roleObject.put("value", new ArrayList<>());
                pushObject.put("role", roleObject);
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);
            }

            pushSetEntity.setPushObject(pushObject.toJSONString());
        } else {
            pushSetEntity = pushSetList.get(0);
            if (Objects.equals(PushTypeEnum.OPPORTUNITY_ADD_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.OPPORTUNITY_ADD_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.OPPORTUNITY_ADD));

            } else if (Objects.equals(PushTypeEnum.OPPORTUNITY_CHANGE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.OPPORTUNITY_CHANGE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.OPPORTUNITY_CHANGE));

            } else if (Objects.equals(PushTypeEnum.OPPORTUNITY_EXPIRE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.OPPORTUNITY_EXPIRE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.OPPORTUNITY_EXPIRE));
            } else if (Objects.equals(PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_MAIN_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_MAIN_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.OPPORTUNITY_TEAM_ADD_DEL_MAIN));
            } else if (Objects.equals(PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_SYNERGETIC_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.OPPORTUNITY_TEAM_ADD_DEL_SYNERGETIC_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.OPPORTUNITY_TEAM_ADD_DEL_SYNERGETIC));
            }
        }
        return pushSetEntity;
    }

    private JSONArray fomatPushSetType(String typeString, PushTypeEnum pushTypeEnum) {
        JSONArray typeArray = new JSONArray();
        switch (pushTypeEnum) {
            case OPPORTUNITY_ADD_PUSH:
                JSONObject object = new JSONObject();
                object.put("name", PushObjectTypeEnum.MAINUSER.getName());
                if (typeString.contains(SPLIT + PushObjectTypeEnum.MAINUSER.getCode() + SPLIT)) {
                    object.put("enable", 1);
                } else {
                    object.put("enable", 0);
                }
                object.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                typeArray.add(object);

                JSONObject object1 = new JSONObject();
                object1.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                if (typeString.contains(SPLIT + PushObjectTypeEnum.COLLABORATOR.getCode() + SPLIT)) {
                    object1.put("enable", 1);
                } else {
                    object1.put("enable", 0);
                }
                object1.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                typeArray.add(object1);
                break;
            case OPPORTUNITY_CHANGE_PUSH:
                JSONObject changeObject = new JSONObject();
                changeObject.put("name", PushObjectTypeEnum.MAINUSER.getName());
                if (typeString.contains(SPLIT + PushObjectTypeEnum.MAINUSER.getCode() + SPLIT)) {
                    changeObject.put("enable", 1);
                } else {
                    changeObject.put("enable", 0);
                }
                changeObject.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                typeArray.add(changeObject);

                JSONObject changeObject1 = new JSONObject();
                changeObject1.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                if (typeString.contains(SPLIT + PushObjectTypeEnum.COLLABORATOR.getCode() + SPLIT)) {
                    changeObject1.put("enable", 1);
                } else {
                    changeObject1.put("enable", 0);
                }
                changeObject1.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                typeArray.add(changeObject1);
                break;
            case OPPORTUNITY_EXPIRE_PUSH:
                break;
            case OPPORTUNITY_TEAM_ADD_DEL_MAIN_PUSH:
                break;
            case OPPORTUNITY_TEAM_ADD_DEL_SYNERGETIC_PUSH:
                break;
            default:
                break;
        }
        return typeArray;
    }

    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        return new JSONObject();
    }



    @Override
    public PushSetEntity getPushSet(String corpid, String attr,
                                    PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }
}
